#!/bin/bash

source $HOME/.bashrc && conda activate lxb39 && cd $PROJECTS/MoE-LLaVA

gpu_list="${CUDA_VISIBLE_DEVICES:-0}"
IFS=',' read -ra GPULIST <<< "$gpu_list"
CHUNKS=${#GPULIST[@]}

CONV="phi"
CKPT_NAME="moe-llava-clip-336-phi-2.7b-3rd-sft-moe-reproduce"
CKPT="${OUTPUTS}/MoE-LLaVA/${CKPT_NAME}"
EVAL="eval_files"

mkdir -p ${EVAL}/mm-vet/answers/${CKPT_NAME}
for IDX in $(seq 0 $((${CHUNKS}-1))); do
    deepspeed --include localhost:${GPULIST[${IDX}]} --master_port $((35987 + ${GPULIST[${IDX}]})) moellava/eval/model_vqa.py \
        --model-path ${CKPT} \
        --question-file ${EVAL}/mm-vet/llava-mm-vet.jsonl \
        --image-folder ${EVAL}/mm-vet/images \
        --answers-file ${EVAL}/mm-vet/answers/${CKPT_NAME}/${CHUNKS}_${IDX}.jsonl \
        --num-chunks ${CHUNKS} \
        --chunk-idx ${IDX} \
        --temperature 0 \
        --conv-mode ${CONV} &
done
wait

output_file=${EVAL}/mm-vet/answers/${CKPT_NAME}/${CKPT_NAME}.jsonl
> "$output_file"
for IDX in $(seq 0 $((${CHUNKS} - 1))); do
    cat ${EVAL}/mm-vet/answers/${CKPT_NAME}/${CHUNKS}_${IDX}.jsonl >> "$output_file"
done

mkdir -p ${EVAL}/mm-vet/results
python3 scripts/convert_mmvet_for_eval.py \
    --src "$output_file" \
    --dst ${EVAL}/mm-vet/results/${CKPT_NAME}.json

python3 moellava/eval/eval_gpt_mmvet.py \
    --mmvet_path ${EVAL}/mm-vet \
    --ckpt_name ${CKPT_NAME} \
    --result_path ${EVAL}/mm-vet/results

